import Vue from 'vue'
import Router from 'vue-router'
import Store from '../store/index'
import { Message, MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
import { format } from 'path';
Vue.use(Router)

var router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: ()=> import('@/views/index/index.vue'),
      meta:{
        login:false,
      }
    },
    {
      path: '/auth/login',
      name: 'login',
      component: ()=> import('@/views/login/index.vue'),
      meta:{
        login:false
      }
    },
    {
      path: '/auth/register',
      name: 'register',
      component: ()=> import('@/views/register/index.vue'),
      meta:{
        login:false
      }
    },
    {
      name:'dashboard',
      path:'/dashboard',
      component: ()=>import ('@/views/layout/index.vue'),
      redirect:'/dashboard/home',
      meta:{
        login:true
      },
      children:[
        {
          name:'home',
          component:()=>import('@/views/home/index.vue'),
          path:'/dashboard/home',
          meta:{
            roles:['admin'],
            login:true
          }
        },
        {
          name:'permission',
          component:()=>import('@/views/permission/index.vue'),
          path:'/dashboard/permission',
          meta:{
            login:true,
            roles:['admin']
          }
        },
        {
          name:'table',
          path:'/dashboard/table',
          component:()=>import('@/views/table/index.vue'),
          redirect:'/dashboard/table/table1',
          meta:{
            login:true,
            roles:['admin']
          },
          children:[{
            name:'table2',
            component:()=>import('@/views/table/components/table1.vue'),
            path:'/dashboard/table/table1',
            meta:{
              login:true,
              roles:['admin']
            }
          },{
            name:'table2',
            component:()=>import('@/views/table/components/table2.vue'),
            path:'/dashboard/table/table2',
            meta:{
              login:true,
              roles:['admin']
            }
          }]
        },
        {
          name:'editor',
          component:()=>import('@/views/editor/index.vue'),
          path:'/dashboard/editor',
          redirect:'/dashboard/editor/articleList',
          meta:{
            login:true,
            roles:['admin']
          },
          children:[
            {
              name:'article',
              component:()=>import('@/views/editor/components/articleList.vue'),
              path:'/dashboard/editor/article',
              meta:{
                login:true,
                roles:['admin']
              }
            },
            {
              name:'edit',
              component:()=>import('@/views/editor/components/edit.vue'),
              path:'/dashboard/editor/edit',
              meta:{
                login:true,
                roles:['admin']
              }
            }]
        }
      ]
    },
    {
      path: '*',
      redirect: '/',
    }
  ]
})

router.beforeEach((to, from ,next)=>{
  let token = Cookies.get('x-token')
    if(token && to.meta.login){
      Store.dispatch('GETUSERINFO', token).then(()=>{
        next()
      })
    }else{
      if(!to.meta.login){
        next()
      }else{
       next(`/auth/login?redirect=${window.location.href}`)
      }
    }
})




export default router
